<?php

class Elective_SubjectController extends Zend_Controller_Action
{
    protected $_year;
    protected $_semester;

    public function init()
    {
        /* Initialize action controller here */
        // 目前的學年學期
        $this->_year = $this->view->Semester['year'];
        $this->_semester = $this->view->Semester['semester'];
    }

    public function indexAction()
    {
    }

    public function saveAction()
    {
        $params = $this->getRequest()->getParams();
        $form = $this->_initForm($params['subject_sn']);
        if ($form->isValid($params)) {
            $electiveGroup = new Student_Model_StudentScoreElectiveGroup();
            if (isset($params['sn']) and $sn = (int) $params['sn'])
            $electiveGroup->assignIdentifier($sn);
            $electiveGroup->fromArray($params);
            $electiveGroup->save();
            $this->_helper->json(array('success'=>true));
        }
        else
        $this->_helper->json(array('success'=>false,'message'=>$form->getMessages()));

    }
    /**
     * 新增群組
     * @return unknown_type
     */
    public function appendGroupAction()
    {

        $this->getHelper('layout')->disableLayout();

        $sn = (int) $this->getRequest()->getParam('sn');

        $this->view->form = $this->_initForm($sn);
        $this->view->subject_sn = $sn;
    }

    private function _initForm($subject_sn)
    {

        $this->init();
        $form = new Elective_Form_ElectiveGroup();
        $q = Doctrine_Query::create()
        ->select('a.sn,a.teacher_sn, a.grade, a.class_sn, b.name AS name')
        ->from('Semester_Model_SemesterTeacherCourse a')
        ->leftJoin('a.Teacher_Model_TeacherView b')
        ->where('a.subject_sn=?', $subject_sn)
        ->orderby('a.class_sn')
        ->groupby('a.teacher_sn');

        $arr = array();
        $semesterClass = new Semester_Model_SemesterClass;
        $classArr = $semesterClass->getClassNames($this->_year,$this->_semester);

        foreach($q->fetchArray() as $row)
        $arr[$row['sn']] = $classArr[$row['grade'].'-'.$row['class_sn']].'--'.$row['name'];
        $form->getElement('teacher_course_sn')->setmultiOPtions($arr);

        return $form;
    }

    /**
     *  取該年級所有課程
     * @return array
     */
    public function getSubjectAction()
    {
        $this->init();
        $grade = (int) $this->getRequest()->getParam('grade');
        $q = Doctrine_Query::create()
        ->from('Semester_Model_SemesterCourse')
        ->where('year=?', $this->_year)
        ->andWhere('semester=?', $this->_semester)
        ->andWhere('grade=?', $grade)
        ->andWhere('special=?','N');
        $res = $q->fetchArray();
        //        echo $this->_year.'--'.$this->_semester.'--'.$grade;
        //        echo $q->getSqlQuery();
        $arr = array();
        foreach($res as $row)
        $arr[$row['sn']] = $row['name'];
        $this->_helper->json($arr);
    }

    /**
     *  分組課程列表
     * @return unknown_type
     */
    public function getCourseAction()
    {
        $sn = (int) $this->getRequest()->getParam('sn');
        $q = Doctrine_Query::create()
        ->select('a.* , c.name AS teacher_name, count(d.sn) AS cc')
        ->from('Student_Model_StudentScoreElectiveGroup a')
        ->leftJoin('a.Student_Model_StudentScoreElectiveStudent d')
        ->leftJoin('a.Semester_Model_SemesterTeacherCourse b')
        ->leftJoin('Teacher_Model_TeacherView c')
        ->where('b.teacher_sn = c.sn')
        ->andwhere('b.subject_sn=?', $sn)
        ->groupBy('a.sn');
        $this->getHelper('layout')->disableLayout();
        $this->view->data = $q->fetchArray();

    }

    public function editGroupAction()
    {
        $this->getHelper('layout')->disableLayout();
        $sn = (int) $this->getRequest()->getParam('sn');
        $subjectSn = (int) $this->getRequest()->getParam('subject_sn');
        $q = Doctrine_Query::create()
        ->from('Student_Model_StudentScoreElectiveGroup')
        ->where('sn=?', $sn);
        //print_r($q->fetchArray());exit;
        if (!$res = $q->fetchOne(null,Doctrine::HYDRATE_ARRAY))
         throw new Exception('Error sn of StudentScoreElectiveGroup');
        $form = $this->_initForm($subjectSn);
        $form->setDefaults($res);
        $this->view->form = $form;
        $this->view->editSn = $res['sn'];
        $this->view->subject_sn = $subjectSn;
        $this->render('append-group');
    }

    public function deleteAction()
    {
        $sn = (int) $this->getRequest()->getParam('sn');
        $q = Doctrine_Query::create()
        ->delete('Student_Model_StudentScoreElectiveGroup')
        ->where('sn=?', $sn);
        $q->execute();
        $this->_helper->json(true);
    }
}

